<?php
class pdmirs {
    
   private $config, $db;
   
   private $config_parameters = array(
            /* Basic parameters */ 
            'basepath',
            /* adodb / database-related parameters */ 
            'adodb_db_type',
            'adodb_debug',
            'adodb_server',
            'adodb_user',
            'adodb_password',
            'adodb_database',
            'adodb_table_prefix',
            'config_modified',
            'lang');
            
    public function __construct() {
	header('Content-Type: text/html; charset=utf-8');
	$this->config['all_parameters_set'] = false;

    }

   // this will be called automatically at the end of scope
    public function __destruct() {
        if ($db) {
            $this->db->Close(); 
        }
    }

    public function connect() {
    	if ($this->config['config_modified'] == false) { die('Please modify your configuration file before trying to connect to a database'); }
        include($this->config['adodb_include_full_path']);

        $this->db = ADONewConnection($this->config['adodb_db_type']);
        $this->db->debug = $this->config['adodb_debug'];
        $this->db->Connect($this->config['adodb_server'], 
                            $this->config['adodb_user'], 
                            $this->config['adodb_password'], 
                            $this->config['adodb_database']);
        $this->db->SetFetchMode(ADODB_FETCH_ASSOC); /* row[coloumname], not row[n]*/
    }

    public function adodb_test() {
        $this->config['adodb_test_table'] = $this->config['adodb_table_prefix'] . 'test';
        $rows = $this->db->Execute('select * from ' . $this->config['adodb_test_table']);
        print "<pre>";
        print_r($rows->GetRows());
        print "</pre>";
    }

    public function set($parameter,$value) {
            switch ($parameter) {
                case 'basepath':
                    $this->config[$parameter] = $value;
                    $this->config['adodb_include_full_path'] = $value .'include/adodb5/adodb.inc.php';
                break;
            
                default:
                    $this->config[$parameter] = $value;
                break;
            }
    }
    
    function check_parameters() {
        if (!$this->config['all_parameters_set']) {
            foreach ($this->config_parameters as $possible_parameter) {
                if (!isset($possible_parameter)) { 
                    echo '<br /><span class="error">Die: Required parameter ' . $possible_parameter . ' not set!</span>'; 
                    die(); 
                }
            }
        }
    }

    public function add_message($melding) {
        $this->config['adodb_test_table'] = $this->config['adodb_table_prefix'] . 'test';
        return $this->db->execute("INSERT INTO '" . $this->config['adodb_test_table'] . "' (message) VALUES ('{$melding}');" );
   }
    public function dbErrorMsg() {
        return $this->db->ErrorMsg();
    }

}
?>
